package com.xqboss.apps.service.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xqboss.apps.constant.LockKey;
import com.xqboss.apps.domain.user.SysUserWalletRecord;
import com.xqboss.apps.domain.user.UserAssetWrench;
import com.xqboss.apps.dto.user.WithdrawRecordListDto;
import com.xqboss.apps.enums.user.WalletChangeDirectionEnum;
import com.xqboss.apps.enums.user.WalletWrenchChangeTypeEnum;
import com.xqboss.apps.mapper.user.UserAssetWrenchMapper;
import com.xqboss.apps.vo.user.SysUserWalletRecordVo;
import com.xqboss.apps.vo.user.WithdrawRecordVo;
import com.xqboss.common.core.redis.lock.LockValue;
import com.xqboss.common.core.redis.lock.RedisLock;
import com.xqboss.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.user.UserAssetWrenchRecord;
import com.xqboss.apps.mapper.user.UserAssetWrenchRecordMapper;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;


/**
 * 用户资产-扳手流水记录(UserAssetWrenchRecord)表服务实现类
 *
 * @author makejava
 * @since 2023-12-07 14:09:52
 */
@Service
public class UserAssetWrenchRecordService extends ServiceImpl<UserAssetWrenchRecordMapper, UserAssetWrenchRecord> {

    @Autowired
    private UserAssetWrenchService userAssetWrenchService;
    @Autowired
    private UserAssetWrenchMapper userAssetWrenchMapper;

    /**
     * 扳手余额修改
     * @param userId
     * @param changeTypeEnum
     * @param directionEnum
     * @param amount
     * @param sourceId
     * @param remark
     * @return 记录id
     */
    @Transactional
    @RedisLock(lockKey = LockKey.LOCK_USER_ASSET_WRENCH)
    public Long updateWrenchBalance(@LockValue Long userId, WalletWrenchChangeTypeEnum changeTypeEnum,
                                    WalletChangeDirectionEnum directionEnum, BigDecimal amount, Long sourceId, String remark) {
        if (amount != null && amount.compareTo(BigDecimal.ZERO) < 1) {
            return  null;
        }
        if (userId == null || changeTypeEnum == null || directionEnum == null
                || amount == null || amount.compareTo(BigDecimal.ZERO) < 1) {
            throw new ServiceException("扳手余额修改参数错误");
        }
        UserAssetWrench wrench = userAssetWrenchService.getByUserId(userId);
        if (directionEnum == WalletChangeDirectionEnum.OUT) {
            if (amount.compareTo(wrench.getAmount()) > 0) {
                throw new ServiceException("扳手余额不足");
            }
            amount = amount.negate();
        }
        boolean result = userAssetWrenchService.lambdaUpdate()
                .setSql("amount = amount +" + amount)
                .eq(UserAssetWrench::getUserId, userId)
                .apply("(amount + {0}) >= 0", amount)
                .update();
        if (result) {
            UserAssetWrenchRecord record = new UserAssetWrenchRecord();
            record.setUserId(userId);
            record.setAmount(amount.abs());
            record.setDirection(directionEnum);
            record.setChangeType(changeTypeEnum);
            record.setSourceId(sourceId);
            record.setRemark(remark);
            record.setAmountBefore(wrench.getAmount());
            record.setAmountAfter(wrench.getAmount().add(amount));
            this.save(record);
            return record.getId();
        }
        throw new ServiceException("扳手更新失败");
    }

    /**
     * 扳手变动记录
     * @param
     * @return
     */
    public List<SysUserWalletRecordVo> getRecordList(WithdrawRecordListDto record) {
//        LambdaQueryWrapper<SysUserWalletRecord> query = new LambdaQueryWrapper<>();
//        query.eq(record.getUserId() != null, SysUserWalletRecord::getUserId, record.getUserId())
//                .eq(record.getChangeType() != null, SysUserWalletRecord::getChangeType, record.getChangeType())
//                .eq(record.getDirection() != null, SysUserWalletRecord::getDirection, record.getDirection())
//                .eq(record.getCreateTimeStart() != null, SysUserWalletRecord::getCreateTime, record.getCreateTimeStart())
//                .eq(record.getCreateTimeEnd() != null, SysUserWalletRecord::getCreateTime, record.getCreateTimeEnd());

        return userAssetWrenchMapper.getRecordList(record);

    }
}
